<?php
namespace app\serverauth\service\auth;

use \app\manage\service\ApiBase;

// 权限服务层  adv30@126.com
class Auth extends ApiBase
{
    private $defaultAuthEdit = ['id'=>'0','name'=>'', 'app_name'=>'', 'server'=>'',
        'module'=>'', 'controller'=>'', 'action'=>'', 'other_check'=>'{}'];
    protected $authValidate = '\app\serverauth\validate\Auth';
    protected $authLogic = '\app\serverauth\logic\Auth';

    // 创建一条权限
    public function add(): array
    {
        $data = $this->issetReceiveData(['name'=>' ', 'app_name'=>'', 'server'=>' ',
            'module'=>' ', 'controller'=>' ', 'action'=>' ', 'other_check'=>'{}']);
        $data['merge_auth'] = $data['server'] . '/' . $data['module'] . '/' .  $data['controller'] . '/' .  $data['action'];
        return $this->oneStepRun($data, 'serveruser/auth/Auth/add', 'add', 'add');
    }
    // 编辑一条权限
    public function edit(): array
    {
        $data = $this->haveIssetReceiveData($this->defaultAuthEdit);
        if(array_eq($data, '', ['name', 'app_name', 'server', 'module', 'controller', 'action'])){
            return \verify_err($data, 'name,app_name,server,module,controller,action参数至少传一个');
        }
        return $this->oneStepRun($data, 'serveruser/auth/Auth/edit', 'edit');
    }
    // 给某个角色一个权限(关系表)
    public function addAuthByRole(): array
    {
        $data = $this->haveIssetReceiveData(['role_id'=>'0', 'auth_id'=>'0']);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/addAuthByRole', 'addAuthByRole', 'addAuthByRole');
    }
        // 给某个角色N个权限(关系表)
    public function addAuthsByRole(): array
    {
        $data = $this->haveIssetReceiveData(['role_id'=>' ', 'auth_ids'=>'0']);
        if ($data['auth_ids'] === '') {
            return \verify_err($data, 'auth_ids');
        }
        return $this->oneStepRun($data, 'serveruser/auth/Auth/addAuthsByRole', 'addAuthsByRole');
    }
    // 解除某个角色的N条权限(关系表,删除)
    public function removeAuthsByRole(): array
    {
        $data = $this->haveIssetReceiveData(['role_id'=>'0', 'auth_ids'=>' ']);
        if ($data['auth_ids'] === '') {
            return \verify_err($data, 'auth_ids');
        }
        return $this->oneStepRun($data, 'serveruser/auth/Auth/removeAuthsByRole', 'removeAuthsByRole');
    }
    // 禁用一条权限
    public function stop(): array
    {
        $data['id'] = $this->arrData['id'] ? $this->arrData['id'] : 0;
        return $this->oneStepRun($data, 'serveruser/auth/Auth/stop', 'stop');
    }
    // 启用一条权限
    public function start(): array
    {
        $data['id'] = $this->arrData['id'] ? $this->arrData['id'] : 0;
        return $this->oneStepRun($data, 'serveruser/auth/Auth/start', 'start');
    }
    // 获取权限列表分页数据
    public function list(): array
    {
        $data = $this->changeKeyData(
                ['page' => 'page', 'list_rows' => 'listRows', 'keywords' => 'keywords'],
                'haveIssetReceiveData',
                ['page' => 1, 'listRows' => '', 'keywords' => '']
            );
        return $this->oneStepRun($data, 'serveruser/auth/Auth/list', 'list');
    }
    // 获取权限列表的树 结构: role->server->module->controller->action
    public function tree(): array
    {
        return $this->oneStepRun([], 'serveruser/auth/Auth/tree', 'tree');
    }
    // 禁用某个角色的所有权限
    public function stopByRole(): array
    {
        $data = $this->haveIssetReceiveData(['role_id'=>'0']);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/stopByRole', 'stopByRole');
    }
    // 启用某个角色的所有权限
    public function startByRole(): array
    {
        $data = $this->haveIssetReceiveData(['role_id'=>'0']);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/startByRole', 'startByRole');
    }
    // 获取某个角色的权限列表分页
    public function listByRole(): array
    {
        $data = $this->haveIssetReceiveData(['role_id'=>'0', 'page'=>'1', 'list_rows'=>'']);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/listByRole', 'listByRole');
    }
    // 获取某个角色的权限所有权限列表数据
    public function treeByRole(): array
    {
        $data = $this->haveIssetReceiveData(['role_id'=>'0']);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/treeByRole', 'treeByRole');
    }
    // 获取指定ids的权限
    public function listByIds(): array
    {
        $data = $this->haveIssetReceiveData(['ids' => '0']);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/listByIds', 'listByIds');
    }
    # 在redis中操作
    // 从数据库在读取N个角色的权限,更新在redis中 给一个【auth角色集】 设置某一种或几种权限 auth-visitor,buyer:并设置时间
    // 获取一个角色集的权限,如没有则从数据库中更新一次
    public function getAuthsByRoles(): array
    {
        $data = $this->haveIssetReceiveData(['roles' => ' ']);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/getAuthsByRoles', 'getAuthsByRoles');
    }
    //通过权限id获取 拥有该权限的角色列表和未拥有角色列表
    public function roleByAuth(): array
    {
        $data = $this->haveIssetReceiveData(['auth_id' => ' ']);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/roleByAuth', 'roleByAuth');
    }
    //根据权限id获取权限详情
    public function authInfo(): array 
    {
        $data = $this->haveIssetReceiveData(['id' => 0]);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/authInfo');
    }
    // 获取某个角色未拥有的权限列表分页
    public function noListByRole(): array 
    {
        $data = $this->haveIssetReceiveData(['role_id'=>'0', 'page'=>'1', 'list_rows'=>'']);
        return $this->oneStepRun($data, 'serveruser/auth/Auth/noListByRole', 'listByRole');
    }
}